<template>
    <div>
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">

            <el-form-item label="权限">
                <el-select v-model="searchForm.roleId" placeholder="按权限搜索" clearable>
                    <el-option v-for="item in typeOption" :key="item.id" :label="item.description"
                        :value="item.id"></el-option>

                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchSubmit">查询</el-button>
                <el-button type="primary" @click="openUserRole">设定用户权限</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="questionList" v-if="questionList.length">
  
            <el-table-column prop="id" label="用户id" >
            </el-table-column>
            <el-table-column prop="type" label="权限" >
                <template slot-scope="scope">
    {{scope.row.role|byValue(typeOption,{value:'id',label:'description'})}}
      </template>
            </el-table-column>
            <el-table-column prop="nickname" label="用户名">
            </el-table-column>
            
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button @click="opeUpdateUserRole(scope.row)" type="text">修改权限</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-empty description="暂无数据" v-else></el-empty>
        <div class="pagination"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.size"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination></div>


        <el-dialog title="设定用户权限" :visible.sync="dialogFormVisible">
            <el-form :model="form" ref="form">
                <el-form-item label="用户id:" label-width="140px">
                    <el-input v-model="form.userId" :disabled="isUpdate"></el-input>
                </el-form-item>
                <el-form-item label="权限类型" label-width="140px">
                    <el-select v-model="form.roleId" placeholder="请选择权限">
                        <el-option v-for="item in typeOption" :key="item.id" :label="item.description"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>





            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="changeRole">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getUserlist, getRoleType, changeRole } from "@/api/role"
export default {
    name: 'HelloWorld',
    props: {
        msg: String
    },
    data() {
        return {
            questionList: [],
            total: 0,
            totalPage: 0,
            isUpdate:false,
            searchForm: {
                typeId: "",
                page: 1,
                size: 10
            },
            form: {
                userId: "",
                roleId: ""

            },
            typeOption: [],
            dialogFormVisible: false
        }
    },
    created() {
        this.getQuestion(this.searchForm);
        this.getRoleType();
    },
    methods: {
        async getQuestion(form) {
            await getUserlist(form).then(res => {

                this.questionList = res.data.data.contentList
                this.total = res.data.data.totalNum
                this.totalPage = res.data.data.totalPage
                // form = { typeId: "" }
            }).catch(error => {
                console.log(error)
            })
        },
        getRoleType() {
            getRoleType().then(res => {

                this.typeOption = res.data.data

            }).catch(error => {
                console.log(error)
            })
        },
        searchSubmit() {
            this.searchForm.page = 1;
            this.getQuestion(this.searchForm);
        },
        handleChecked(val, item) {
            console.log(val, item)
        },
        openUserRole() {
            this.isUpdate=false;

            this.dialogFormVisible = true;
            this.resetRoleForm();
        },
        opeUpdateUserRole(row) {
            this.form.userId=row.id;
            this.form.roleId=row.role;
            this.isUpdate=true;
            this.dialogFormVisible = true;
        },
        updateRole() {
                changeRole(this.form).then(async () => {
                    await this.getQuestion(this.searchForm);
                  
                    // this.$message({
                    //     type: 'success',
                    //     message: '删除成功!'
                    // });
                })

        


        },
        resetRoleForm() {
            this.form = {
                "userId": "",
                "roleId": ""
            }
        },
        changeRole() {
            this.dialogFormVisible = false;

            changeRole(this.form).then(async () => {
                this.dialogFormVisible = false;
                await this.getQuestion(this.searchForm);
               
            })
        },
        handleSizeChange(val) {
            this.searchForm.size = val
            this.getQuestion(this.searchForm);
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.searchForm.page = val
            this.getQuestion(this.searchForm);

            console.log(`当前页: ${val}`);
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
    margin: 40px 0 0;
}

ul {
    list-style-type: none;
    padding: 0;
}

li {
    display: inline-block;
    margin: 0 10px;
}

a {
    color: #42b983;
}

.pagination {
    margin-top: 10px;
    float: right;
}
</style>